﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
namespace DanhMuc
{
    public partial class frmBangCap : XFormBase
    {
        BangCapInfo ObjBangCap = new BangCapInfo();
        BangCapCtr CtrObj = new BangCapCtr();
        enum ActionMode
        {
            New,Edit,Del
        }
        ActionMode eAction = ActionMode.New;
        public frmBangCap()
        {
            InitializeComponent();
            InitGrid();
            Init();

            //btnEdit.Visible = false;
        }
        public void Init()
        {
            //kiểm tra nếu là textbox thì cho phép di chuyển tới control ke tiep bang phim enter.
            //foreach (Control item in pnlInput.Controls)
            //{
            //    if (item.GetType() == typeof(TextEdit))
            //    {
            //        ((TextEdit)item).EnterMoveNextControl = true;
            //    }
            //}
            //txtBangCapID.Focus();
            //txtBangCapID.Properties.CharacterCasing = CharacterCasing.Upper;
        }

        public void ClearForm()
        {
            txtBangCapID.Text = string.Empty;
            txtTenBangCap.Text = string.Empty;
            checkActive.Checked = true;
            txtBangCapID.Focus();
            txtBangCapID.Properties.CharacterCasing = CharacterCasing.Upper;
        }
        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[3];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Bằng Cấp";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "BangCapID";
            //Cols[i].OptionsColumn.ReadOnly = true;

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên Bằng Cấp";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TenBangCap";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Sử Dụng";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "Active";


            this.gridView1.Columns.AddRange(Cols);

            //GridColumn gridColumn2 = new GridColumn();
            //this.gridView1.Columns.AddRange(Cols);
            //this.gridView1.GridControl = this.gridControl1;
            //this.gridView1.Name = "gridView1";
            //this.gridView1.OptionsView.ColumnAutoWidth = false;
            //this.gridView1.IndicatorWidth = 30;
            this.gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            //ColumnFilterInfo            
            this.gridView1.OptionsView.ShowGroupPanel = false;
            this.gridView1.OptionsView.ShowAutoFilterRow = true;
            this.gridControl1.DataSource = CtrObj.GetList();
        }


        public void Save()
        {
            DataTable DTBangCap = (DataTable)gridControl1.DataSource;
            foreach (DataRow Item in DTBangCap.Rows)
            {
                
                if (Item.RowState == DataRowState.Added)
	            {
                    ObjBangCap = new BangCapInfo();
                    ObjBangCap.BangCapID = Item["BangCapID"].ToString();
                    ObjBangCap.TenBangCap = Item["TenBangCap"].ToString();
                    ObjBangCap.Active = Convert.ToBoolean(Item["Active"]);
                    CtrObj.Insert(ObjBangCap);
                    //MessageBox.Show(string.Format("Đã thêm: {0}", Item[0].ToString()));                    
	            }
                if (Item.RowState == DataRowState.Modified)
                {
                    ObjBangCap = new BangCapInfo();
                    ObjBangCap.BangCapID = Item["BangCapID",DataRowVersion.Original].ToString();
                    ObjBangCap.TenBangCap = Item["TenBangCap"].ToString();
                    ObjBangCap.Active = Convert.ToBoolean(Item["Active"]);
                    CtrObj.Update(ObjBangCap);
                    //MessageBox.Show(string.Format("Đã sửa: {0}", Item[0].ToString()));
                }
            }
            this.gridControl1.DataSource = CtrObj.GetList();
            MessageBox.Show("Đã cập nhật xong");
        }



        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            //ObjBangCap.BangCapID = txtBangCapID.Text;
            //ObjBangCap.TenBangCap = txtTenBangCap.Text;
            //ObjBangCap.Active = checkActive.Checked;
            //if (eAction == ActionMode.New) //them moi
            //{
            //    try
            //    {
            //        CtrObj.Insert(ObjBangCap);
            //        this.gridControl1.DataSource = CtrObj.GetList();
            //        ObjBangCap = new BangCapInfo();
            //    }
            //    catch (Exception ex)
            //    {

            //        MessageBox.Show(string.Format("Có lỗi: {0}", ex.Message));
            //        return;
            //    }

            //    if (MessageBox.Show("Bạn muốn thêm tiếp không?", "tiếp", MessageBoxButtons.YesNo) == DialogResult.Yes)
            //    {
            //        ClearForm();
            //    }
            //}
            //else
            //{
            //    try
            //    {
            //        CtrObj.Update(ObjBangCap);
            //        this.gridControl1.DataSource = CtrObj.GetList();
            //    }
            //    catch (Exception ex)
            //    {

            //        MessageBox.Show(string.Format("Có lỗi: {0}", ex.Message));
            //        return;
            //    }

            //}
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            eAction = ActionMode.New;
            ClearForm();

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void frmBangCap_Load(object sender, EventArgs e)
        {

        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode== Keys.Delete)
            {
                //MessageBox.Show("press delete key");
                if (this.gridView1.SelectedRowsCount>0)
                {
                    string strBangCapID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "BangCapID").ToString();
                    if (MessageBox.Show("Bạn có muốn xóa bằng cấp đã chọn không?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        CtrObj.Delete(strBangCapID);
                        this.gridControl1.DataSource = CtrObj.GetList();
                    }
                }
            }
        }
    }
}